﻿using System.Collections.Generic;
using System.Collections;
using System;

namespace ClientAPI
{
    class ChatDictionary
    {
        private static ChatDictionary _instance;
        private Dictionary<string, ChatLog> _dictionary;
        private ArrayList _userList = new ArrayList();

        private ChatDictionary()
        {
            string all = "ALL";
            ChatLog chatlog = new ChatLog();
            chatlog.AddMessage("Welcome to FrankenChat! Time: " + DateTime.Now.ToString());

            _dictionary = new Dictionary<string, ChatLog>();

            _dictionary.Add(all, chatlog);
            _userList.Add(all);
        }

        public static ChatDictionary Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new ChatDictionary();
                }
                return _instance;
            }
        }

        public Dictionary<string, ChatLog> GetChatDictionary()
        {
            return _dictionary;
        }

        public void SetChatDictionary(string key, string message)
        {
            ChatLog tempArray = new ChatLog();
            if (message != null)
            {
                if (_dictionary.ContainsKey(key))
                {
                    _dictionary.TryGetValue(key, out tempArray);
                    _dictionary.Remove(key);
                    tempArray.AddMessage(message);
                    _dictionary.Add(key, tempArray);
                }
                else
                {
                    tempArray.AddMessage(message);
                    _dictionary.Add(key, tempArray);
                }
            }
        }

        public void AddNewLogin(string key)
        {
            bool hasValue = false;

            foreach (string i in _userList.ToArray(typeof(string)))
            {
                if (i == key)
                    hasValue = true;
            }

            if(!hasValue)
                _userList.Add(key);
        }

        public ArrayList GetUserList()
        {
            return _userList;
        }

        public void Close(string key)
        {
            _dictionary.Remove(key);
        }
    }
}
